package com.ipt.frm.pw.listener;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * Application Lifecycle Listener implementation class ContextListener
 * 
 */
public class ContextListener implements ServletContextListener {

	private static final String ATTRIBUTE_NAME = "mensaje";
	public static Properties propertiesMsg;
	static final private String PATH_PROPERTIES_MSG = "/WEB-INF/config/messages.properties";

	/**
	 * @see ServletContextListener#contextInitialized(ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent event) {
		propertiesMsg = new Properties();
		try {
			propertiesMsg.load(event.getServletContext().getResourceAsStream(
					PATH_PROPERTIES_MSG));
		} catch (IOException e) {
			e.printStackTrace();
		}
		event.getServletContext().setAttribute(ATTRIBUTE_NAME, this);
	}

	/**
	 * @see ServletContextListener#contextDestroyed(ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent event) {
		propertiesMsg = null;
	}

	public static ContextListener getInstance(ServletContext context) {
		return (ContextListener) context.getAttribute(ATTRIBUTE_NAME);
	}

	public String getProperty(String key) {
		return propertiesMsg.getProperty(key);
	}

}
